package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.dao.ActivityPlanDao;
import com.qdlc.p2p.dal.dto.ActivityPlan;
import com.qdlc.p2p.dal.model.ActivityPlanModel;
/**
 * 活动方案DAO实现
 * @author zlhx
 * @version 2.0
 * @date 2015-04-24 10:07
 */
@Repository(value="activityPlanDao")
public class ActivityPlanDaoImpl extends BaseDaoImpl<ActivityPlan> implements ActivityPlanDao {

	@SuppressWarnings("unchecked")
	@Override
	public ActivityPlan getActivityPlanByNid(String nid) {
		String sql = "from ActivityPlan where nid = ?";
		Query query = em.createQuery(sql);
        query.setParameter(1, nid);
        List<ActivityPlan> list = query.getResultList();
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
	}

	@Override
	public int update(int status, int prestatus,long id) {
		String jpql = "UPDATE ActivityPlan set status=:status WHERE status=:prestatus AND id=:id";
		int count = updateByJpql(jpql, new String[]{"status","prestatus","id"}, new Object[] {status,prestatus,id});
		return count;
	}
	
	@Override
	public int modify(ActivityPlanModel model ) {
		String sql = "UPDATE " + Global.DB_PREFIX + "activity_plan set status=:status,name=:name,red_envelope_rule=:red_envelope_rule,"
				+ "experience_rule=:experience_rule,uprate_rule=:uprate_rule,"
				+ "start_time=:start_time,end_time=:end_time "
				+ " WHERE id=:id";
		int count = updateBySql(sql, new String[]{"status","name","red_envelope_rule","experience_rule",
				"uprate_rule","start_time","end_time","id"}, 
				new Object[] {model.getStatus(),model.getName(),model.getRedEnvelopeRuleId(),model.getExperienceRuleId(),
				model.getRateRuleId(),model.getStartTime(),model.getEndTime(),model.getId()});
		return count;
	}
	
	
}
